﻿Chenxin.UI.ViewHouse = Chenxin.extend(Chenxin.UI.PageCommon, {
    autoInitForm: false,
    actionUrl: 'service/viewHouse.ashx',
    createGrid: function() {
        this.PageViewHouseLiseGrid = new Chenxin.UI.Pagination({
            actionUrl: this.actionUrl,
            domId: 'page',
            pageSize: 10,
            renderTo: 'view_table',
            data: { op: "search" },
            render: function(data) {
                document.getElementById('view_table').innerHTML = data;
            }
        });
        this.ViewHouseListGrid = new Chenxin.UI.Pagination({
            actionUrl: this.actionUrl,
            pageSize: 5,
            data: { op: "getlist" },
            autoLoad: false,
            render: function(data) {
                document.getElementById('viewlist_table').innerHTML = data;
            }
        });
    },
    createSearch: function() {
        return new Chenxin.UI.ViewHouseSearch({
            scope: this,
            afterGetParamJson: function(data) {
                data.op = 'search';
            }
        });
    },
    createForm: function() {
        return new Chenxin.UI.ViewHouseForm({

            scope: this,
            alwaysCheckPermit: true,
            beforeFitData: function(model) {
                if (this.form.op != 'add') {
                    this.form.cmbviewuser.param.fatherid = model.DepartId;
                    this.form.cmbviewuser.reload(model.ReportorId);
                }
            },
            afterDelSucc: function() {
                this.form.autoHide = true;
                if (this.form.op == 'add') {
                    this.ViewHouseListGrid.reload({ op: "getlist" });
                }
                else {
                    this.ViewHouseListGrid.reload({ op: "getlist", RecordId: this.currentId });
                    this.PageViewHouseLiseGrid.reload();
                }
            },
            saveSucc: function() {
                this.PageViewHouseLiseGrid.reload();
            }
        });
    },
    loadinfo: function(id) {
        var _this = this;
        Chenxin.ajax({
            type: "get",
            dataType: "text",
            paramType: 'json',
            url: 'service/CustomerMgr.ashx',
            data: { op: 'getinfo', modelid: id },
            onSuccess: function(data, msg) {
                if (data.model) {
                    _this.currentCustomerInfo = data.model;
                    _this.showTraceWin();
                }
            }
        });
    },
    showViewTrace: function(id) {
        _this = this;
        _this.customerid = id;
        if (!this.vWin) {
            this.vWin = new Chenxin.UI.ViewTraceWin({
                scope: this,
                title: '跟单列表',
                op: 'viewtrace'
            });
        }
        if (this.vWin.show()) {
            this.vWin.grid.reload({ customerid: id });
        }
    },
    showTraceWin: function() {
        var _this = this;
        if (!this.traceWin) {
            this.traceWin = new Chenxin.UI.CustomerTraceForm({
                scrop: _this,
                afterGetFormJson: function(json) {
                    delete json.AddPerson;
                    delete json.AddDepart;
                    json.HouseAddress = json.Address;
                    json.HouseId = _this.currentCustomerInfo.CustomerId;
                    json.HouseCode = _this.currentCustomerInfo.CustomerCode;
                    json.HouseAddress = _this.currentCustomerInfo.CustomerName;
                    json.status = 1;
                    delete json.Address;
                    delete json.CustomerCode;

                    if (this.isNotice) {
                        this.additionalParam.noticemodel = {
                            body: json.body,
                            NoticeTime: json.SendDate,
                            HouseAddress: _this.currentCustomerInfo.CustomerName
                        };
                        delete json.body;
                        delete json.SendDate;
                    } else {
                        this.additionalParam = {};
                    }
                    this.additionalParam.isnotice = json.isnotice || '0';
                    delete json.isnotice;
                }
            });
        }
        this.traceWin.show();
        $('#tr_noticetime').hide();
        $('#tr_noticecontent').hide();
        var _data = this.currentCustomerInfo;
        _data.HouseCode = this.currentCustomerInfo.CustomerCode;
        _data.AddPerson = this.currentCustomerInfo.OprPerson;
        _data.AddDepart = this.currentCustomerInfo.OprDepart;
        this.traceWin.fitData(_data);
        this.traceWin.setTitle('客源跟单');
    },
    initEvent: function() {
        var _this = this;

        //生成带看单
        $('#btn_addview').live("click", function() {
            _this.form.op = 'add';
            _this.form.reset();
            if (_this.form.show()) {
                _this.form.setTitle('生成带看单');
                _this.ViewHouseListGrid.reload({ op: "getlist" });
            }

        });
        //跟单
        $('#btn_trace').live("click", function() {
            _this.loadinfo(this.name);
        });
        //跟单记录
        $('#btn_viewtrace').live('click', function() {
            _this.showViewTrace(this.name);
        });
        //详情
        $('#btn_viewhouseinfo').live('click', function() {
            _this.currentId = this.name;
            _this.form.permitParam = { currentId: _this.currentId };
            _this.view();
            _this.form.setTitle('查看带看单');
            _this.ViewHouseListGrid.reload({ op: "getlist", RecordId: _this.currentId, type: "detail" });
        });
        //编辑
        $('#btn_viewhouseedit').live('click', function() {
            _this.currentId = this.name;
            _this.form.permitParam = { currentId: _this.currentId };
            _this.edit();
            _this.form.setTitle('编辑带看单');
            _this.ViewHouseListGrid.reload({ op: "getlist", RecordId: _this.currentId });
        });
    },
    initComponent: function() {
        Chenxin.UI.ViewHouse.superclass.initComponent.call(this);
    }
});

/**
*查询面板
*/
Chenxin.UI.ViewHouseSearch=Chenxin.extend(Chenxin.UI.SearchCommon,{
    id:'view_search',
	initEvent:function(){
		var _this=this; 
        
		//查询按钮
		$('#btnSreachView').click(function(){
		    var data = _this.scope.search.getParamJson();
		    _this.scope.PageViewHouseLiseGrid.reload(data);
		});
	
	},
    createComboList: function() {
    
        //五级联Combo
        var combl = new Chenxin.UI.StructComboList();
        var structs = combl.comboList;
        
        return structs;
    },
    initComponent: function() {
        Chenxin.UI.ViewHouseSearch.superclass.initComponent.call(this);
    }
});

/**
*@description 带看表单
*/
Chenxin.UI.ViewHouseForm = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: 'service/viewHouse.ashx',
    id: 'jqmviewhouse',
    renderTo: 'divviewhouse',
    autoHide: true,
    jqmWidth: 700,
    initFormValidator: function() {
        var _this = this;

        //客户名
        $("#CustomerName").formValidator({
            validatorgroup: this.id,
            onshow: "请输入用户姓名",
            onfocus: "请输入用户姓名",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: { leftempty: false, rightempty: false, emptyerror: "用户姓名不能为空" },
            onerror: "用户姓名不能为空"
        });
        //带看时间
        $("#ViewTime").focus(function() {
            WdatePicker({ oncleared: function() { $(this).blur(); }, onpicked: function() { $(this).blur(); } })
        }).formValidator({ validatorgroup: this.id,
            onshow: "请输入带看时间",
            onfocus: "请输入带看时间",
            oncorrect: " "
        }).inputValidator({
            empty: { leftempty: false, rightempty: false, emptyerror: "带看时间不能为空" },
            type: "date",
            onerror: "请输入带看时间"
        });
        //证件号
        $("#CertificateNo").formValidator({
            validatorgroup: this.id,
            onshow: "请输入证件号",
            onfocus: "请输入证件号",
            oncorrect: " "
        }).functionValidator({
            fun: function(val, elem) {
                if (_this.comboList[0].getText() == '身份证') {
                    return isCardID(val);
                } else {
                    return true; //val.isBlank();
                }
            },
            oncorrect: " ",
            onshow: " ",
            onerror: "请输入正确的证件号"
        });
        //电话
        $("#Phone").formValidator({ validatorgroup: this.id,
            onshow: "请输入电话", onfocus: "请输入电话", oncorrect: " "
        }).functionValidator({
            fun: function(val, elem) {
                if ($("#Phone").val() == "" && $("#Mobile").val() == "") {
                    return false;
                } else {
                    return true;
                }
            }, onerror: "请输入手机号码或电话"
        });
        //手机号码
        $("#Mobile").formValidator({ validatorgroup: this.id,
            onshow: "请输入手机号", onfocus: "请输入手机号", oncorrect: " "
        }).functionValidator({
            fun: function(val, elem) { if (val == "") return true; return val.isMobile(); }, onerror: "请输入正确的手机号"
        }).functionValidator({
            fun: function(val, elem) {
                if ($("#Phone").val() == "" && $("#Mobile").val() == "") {
                    return false;
                } else {
                    return true;
                }
            }, onerror: "请输入手机号码或电话"
        });

    },
    createForm: function() {
        return formCommon.ViewHouseForm('viewhouse');
    },
    createComboList: function() {
        var _this = this;

        //证件类型
        var certificateType = new Chenxin.UI.ParameterCombo({
            containerId: '#viewhouse_CertificateType',
            name: 'CertificateType',
            key: 'Custom.CertificateType',
            inModelType: 'txt',
            tbxWidth: 95,
            onSelected: function() {
                $("#CertificateNo").blur();
            }
        });
        //国籍
        var nationality = new Chenxin.UI.Combo({
            containerId: '#viewhouse_Nationality',
            name: 'Nationality',
            inModelType: 'txt',
            tbxWidth: 95,
            items: comboCommon.country
        });

        //带看人部门
        this.cmbviewdepart = new Chenxin.UI.StructCombo({
            param: { op: 'getcontroldeparts' },
            containerId: '#divViewDepartment',
            inModelType: 'all',
            name: 'DepartId',
            txtModelName: "ReportDepart",
            tbxWidth: 90,
            colWidth: 100,
            autoLoad: true,
            required: true,
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    _this.cmbviewuser.clearItems();
                } else {
                    _this.cmbviewuser.param.fatherid = this.getValue();
                    _this.cmbviewuser.reload();
                }
            }
        });
        //带看人
        this.cmbviewuser = new Chenxin.UI.StructCombo({
            autoLoad: false,
            param: { op: 'getemployee' },
            inModelType: 'all',
            name: 'ReportorId',
            txtModelName: "Reportor",
            tbxWidth: 90,
            colWidth: 100,
            containerId: '#divViewUser',
            required: true
        });
        return [certificateType, nationality, this.cmbviewdepart, this.cmbviewuser];
    },
    initEvent: function() {
        var _this = this;
        //提交
        $('#viewhouse_Save').live('click', function() {
            //_this.op="addview";
            _this.save();
        });
        //取消
        $('#viewhouse_Cancle').live('click', function() {
            _this.hide();
        });
        //删除缓存带看单房源
        $('#btn_delcacheview').live("click", function() {
            _this.autoHide = false;
            _this.del(this.name, "delcache");
        });
        //删除数据库带看单房源
        $('#btn_deldbview').live("click", function() {
            _this.autoHide = false;
            _this.del(this.name, "deldb");
        });
    },
    initComponent: function() {
        Chenxin.UI.ViewHouseForm.superclass.initComponent.call(this);
    }
});

Chenxin.UI.ViewTraceWin = Chenxin.extend(Chenxin.UI.FormCommon, {
    id: 'viewrecord',
    renderTo: 'jqm_viewtrace',
    jqmWidth: 550,
    actionUrl: 'service/viewHouse.ashx',
    createGrid: function() {
    return new Chenxin.UI.Pagination({
            pageSize: 10,
            actionUrl: this.actionUrl + '?op=getcustomertracelist',
            renderTo: 'trace_list1',
            data: { customerid: _this.customerid }
        });
    },
    createForm: function() {
        return Chenxin.FormCommon.viewTraceForm(this.id);
    },
    initEvent: function() {
        var _this = this;
        $('#' + this.id + '_Cancle').live('click', function() {
            _this.hide();
        });
    },
    initComponent: function() {
        Chenxin.UI.ViewTraceWin.superclass.initComponent.call(this);
        this.grid = this.createGrid();
    }
});
/**
*@description 跟单列表
*@method 提交按钮的处理方法
*/
Chenxin.FormCommon.viewTraceForm = function(comboIdPrefix) {
return '<table style="width:100%"> <tbody><tr><div id="trace_list1" ></div></tr><tr><td align="right"><div style="float:right; margin-right:15px;"><a id="' + comboIdPrefix + '_Cancle"  class="btn_n fl" href="#"><span>取消</span></a></div></td></tr></tbody></table>'
}


$(document).ready(function(){
    var ViewHouse=new Chenxin.UI.ViewHouse();
});